package com.demo.passenger.sys.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.passenger.sys.user.entity.SysUser;
import com.demo.passenger.sys.user.mapper.SysUserMapper;
import com.demo.passenger.sys.user.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weifucheng
 * @since 2019-07-13
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public List<SysUser> listByParams(Page<SysUser> page, SysUser sysUser) {
        return this.baseMapper.listByParams(page,sysUser);
    }
    @Override
    public List<SysUser> listByParams(SysUser sysUser) {
        Page<SysUser> page=new Page<SysUser>();
        page.setSize(Integer.MAX_VALUE);
        return this.baseMapper.listByParams(page,sysUser);
    }

    @Override
    public SysUser readByUserName(String userName) {
        SysUser sysUser=new SysUser();
        sysUser.setLoginName(userName);
        List<SysUser> users=this.listByParams(sysUser);
        if(users.size()==0){
            return null;
        }
        return users.get(0);
    }

    @Override
    public Page<SysUser> listByParams(SysUser sysUser, Integer current, Integer size) {
        Page<SysUser> page=new Page<SysUser>();
        if(current==null){
            current=1;
        }
        if(size==null){
            size=10;
        }
        page.setSize(size);
        page.setCurrent(current);
        page.setRecords(this.baseMapper.listByParams(page,sysUser));
        return page;
    }

    @Override
    public Boolean saveSingle(SysUser sysUser) {
        //新增时判断登录名是否唯一
        if(StrUtil.isEmpty(sysUser.getId())){
            SysUser params=new SysUser();
            params.setLoginName(sysUser.getLoginName());
            Integer count=this.listByParams(params).size();

            //已存在
            if(count>0){
                return false;
            }
        }

        return this.saveOrUpdate(sysUser);
    }

}
